package com.base.git;

import org.eclipse.jgit.api.*;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.revwalk.*;
import org.eclipse.jgit.revwalk.filter.*;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.File;
import java.util.List;

public class GitBranchDemo {

    public static void main(String[] args) throws Exception {
        // 指定本地仓库目录路径
        String localRepositoryPath = "D:\\java\\work\\base-demo";

        // 打开本地仓库
        Repository repository = new FileRepositoryBuilder()
                .setGitDir(new File(localRepositoryPath + "/.git"))
                .build();

        // 创建 Git 对象
        Git git = new Git(repository);

        // 获取所有分支
        List<Ref> branches = git.branchList().call();

        System.out.println("Branches:");

        // 打印所有分支的名字
        for (Ref branch : branches) {
            String branchName = branch.getName();
            System.out.println(branchName);
        }

        // 关闭 Git 和 Repository 对象
        git.close();
        repository.close();
    }
}